package com.example.wujinapi.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wujinapi.comm.Constant;
import com.example.wujinapi.comm.R;
import com.example.wujinapi.comm.jwtUtils;
import com.example.wujinapi.comm.wechat.WxSignUtils;
import com.example.wujinapi.dao.UsersDao;
import com.example.wujinapi.entity.Users;
import com.example.wujinapi.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * (Users)表服务实现类
 *
 * @author makejava
 * @since 2024-03-25 02:24:51
 */
@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {
    @Autowired
    private UsersDao usersDao;


    /*@Override
    public List<Map<String, Object>> getNews(int pageNum, int pageSize) {
        Page<News> page = new Page<News> (pageNum,pageSize); //实现定义的接口
        List<Map<String,Object>> SharesData=newsDao.selectPageVo(page);
        return SharesData;
    }*/


    @Override
    public R wxDecryptPhone(String token, String encryptedData, String iv) throws Exception {

        try{
            //1、解密token
            //userid
            //openid
            //SessionKey
            String userid= jwtUtils.getToken(token).getClaim("userid").toString().replace("\"","");
            String openid=jwtUtils.getToken(token).getClaim("openid").toString().replace("\"","");
            String sessionKey= jwtUtils.getToken(token).getClaim("SessionKey").toString().replace("\"","");
            //System.out.println("解密获取sessionKey:===>"+sessionKey);
            String data= WxSignUtils.wxDecrypt(encryptedData, sessionKey, iv);
            com.alibaba.fastjson.JSONObject returnStr= com.alibaba.fastjson.JSONObject.parseObject(data);
            //用户手机号码
            String userPhone=returnStr.getString("phoneNumber");
            //System.out.println("获取用户手机密码:==>"+userPhone);

            if(!StringUtils.isEmpty(userid) && !StringUtils.isEmpty(userPhone)) {
                Users users = new Users();
                users.setId(Integer.parseInt(userid));
                users.setUserphone(userPhone);
                usersDao.updateById(users);
            }

            //读取用户最新信息
            Users userinfo=this.readUserByOpenid(openid);

            //设置过期时间
            //微信小程序的sessionKey是三天后过期，必须早于此时间
            //172800=2.5(天)x24(小时)x60(分)x60(秒) (2天)
            int expireTime = Constant.expireTime; //单位：秒 默认172800秒 2天
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("expireTime",expireTime);
            map.put("userinfo",userinfo);
            map.put("token",token);
            return R.ok().put("data",map);

        }catch (Exception ex){
            System.out.println(ex.getMessage());
            return R.error(400,"解密失败");
        }
    }


    @Override
    public void addUser(Users users) {
        Users ur=usersDao.selectOne(new QueryWrapper<Users>().eq("wxopenid", users.getWxopenid()));
        if(ur==null){ //为空才插入新用户
            usersDao.insert(users);
        }
    }

    @Override
    public void updateUserByOpenid(Users users) {
        Users ur=usersDao.selectOne(new QueryWrapper<Users>().eq("wxopenid", users.getWxopenid()));
        if(ur!=null){
            users.setId(ur.getId());
            usersDao.updateById(users);
        }
    }

    @Override
    public void updateUserByUserId(Users users) {
        Users ur=usersDao.selectOne(new QueryWrapper<Users>().eq("id", users.getId()));
        if(ur!=null){
            users.setId(ur.getId());
            usersDao.updateById(users);
        }
    }


    @Override
    public Users readUserById(int id) {
        Users ur=usersDao.selectOne(new QueryWrapper<Users>().eq("id", id).eq("isshow",1)); //1显示 0不显示
        return ur!=null?ur:null;
    }

    @Override
    public Users readUserByOpenid(String openid) {
        Users ur=usersDao.selectOne(new QueryWrapper<Users>().eq("wxopenid", openid).eq("isshow",1));
        return ur!=null?ur:null;
    }

    @Override
    public List<Users> getUsers() {
        List<Users> listUsers=usersDao.selectList(new QueryWrapper<Users>());
        return listUsers!=null?listUsers:null;
    }
}

